package com.hksj.Intelligentmanagement.service;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.hksj.Intelligentmanagement.common.Result;
import com.hksj.Intelligentmanagement.dto.RegionDTO;
import com.hksj.Intelligentmanagement.entity.RegionEntity;
import com.hksj.Intelligentmanagement.mapper.RegionMapper;
import com.hksj.Intelligentmanagement.vo.RegionVo;
import lombok.extern.slf4j.Slf4j;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class RegionService {
    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private Mapper mapper;
    @Autowired
    private Gson gson;

    //新增
    public Result saveRegion(RegionDTO regionDTO) {

        RegionEntity regionEntity =new RegionEntity();

        regionEntity.setRegionName(regionDTO.getRegionName());

        regionEntity.setStatus("0");

        int insert = regionMapper.insert(regionEntity);

        if (insert != 1) {
            throw new RuntimeException("新增失败");
        }

        log.info(gson.toJson(regionEntity));

        return Result.ok("新增区域信息成功");
    }

    //删除
    public Result delRegion(String id) {
        //根据id查询区域信息是否存在
        RegionEntity regionEntity = regionMapper.selectById(id);

        if (Objects.isNull(regionEntity)) {
            //不存在
            return Result.error("id输入有误,区域不存在");
        }

        //存在,逻辑删除
        int i = regionMapper.deleteById(id);

        if (i != 1) {
            throw new RuntimeException("删除失败");
        }

        return Result.ok("删除区域信息成功");

    }

    //修改/更新
    public Result updateRegion(RegionDTO regionDTO) {
        //根据id查询区域信息是否存在
        RegionEntity regionEntity = regionMapper.selectById(regionDTO.getId());

        if (Objects.isNull(regionEntity)) {
            //不存在
            return Result.error("输入的id有误,修改失败");
        }

        //存在
        //将regionDTO转化为RegionEntity
        regionEntity = mapper.map(regionDTO, RegionEntity.class);

        //做更新
        int i = regionMapper.updateById(regionEntity);

        if (i != 1) {
            throw new RuntimeException("更新失败");
        }

        return Result.ok("更新区域信息成功");

    }

    //根据id查询区域信息
    public Result queryRegionById(String id) {
        //根据id查询区域信息是否存在
        RegionEntity regionEntity = regionMapper.selectById(id);

        if (Objects.isNull(regionEntity)) {
            //不存在
            return Result.error("id输入有误,查询区域信息失败");
        }

        return Result.ok(regionEntity);
    }


    //分页查询所有
    public IPage<RegionVo> selectAllRegion(RegionDTO regionDTO) {
        //创建一个分页实例
        IPage<RegionVo> page = new Page<>(regionDTO.getPageNum(), regionDTO.getPageSize());

        //分页查询
        List<RegionVo> regionVoList = regionMapper.queryRegionListByPage(page, regionDTO);

        //将分页结果设置到分页中
        page.setRecords(regionVoList);

        //返回分页
        return page;
    }
}
